package com.gitee.zw.irmp.common.auth.session;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gitee.zw.irmp.common.auth.vo.LoginUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

@Slf4j
@Configuration
@ConditionalOnProperty(value = "app.irmp.common.auth.session-type", havingValue = "redis", matchIfMissing = false)
public class RedisSessionConfig {

    @Bean
    public RedisTemplate<String, LoginUser> redisTemplate(LettuceConnectionFactory connectionFactory, ObjectMapper objectMapper) {
        RedisTemplate<String, LoginUser> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);
        Jackson2JsonRedisSerializer<LoginUser> serializer = new Jackson2JsonRedisSerializer<>(objectMapper, LoginUser.class);
        template.setDefaultSerializer(serializer);
        return template;
    }

    @Bean
    public UserSessionStore userSessionStore() {
        log.info("UserSessionStore is Redis");
        return new UserSessionStoreRedisImpl();
    }
}
